<template>
    <div class="wrapper">
        <van-tabs v-model:active="isDirect" color="#66d6ff" @click-tab="aaa">
            <van-tab title="新增商户">
                <div class="myContent">
                    <div class="top">
                        <div class="dateMon" @click="openPicker">
                            <span>{{ viewDate }}</span>
                            <img src="@/assets/chevron-down@2x.png" alt="" />
                        </div>
                        <div id="myChart0" :style="{ width: '100%', height: '250px' }"></div>
                    </div>
                    <div class="bottom">
                        <div class="actCodeList">
                            <div class="item" v-for="item in actCodeList" :key="item">
                                <div class="con">
                                    <div class="left">
                                        <img src="@/assets/agentImg.png" class="agentImg" alt="" />
                                    </div>
                                    <div class="right">
                                        <div class="name">{{ item.custName }}</div>
                                        <div class="phone">{{ item.phoneNo }}</div>
                                        <div class="phone">{{ item.incrDate }} {{ item.incrTime }}</div>
                                    </div>
                                </div>
                            </div>
                            <empty v-if="!actCodeList.length"></empty>
                        </div>
                    </div>
                </div>
            </van-tab>
            <van-tab title="下级新增">
                <div class="myContent">
                    <div class="top">
                        <div class="dateMon" @click="openPicker">
                            <span>{{ viewDate }}</span>
                            <img src="@/assets/chevron-down@2x.png" alt="" />
                        </div>
                        <div id="myChart1" :style="{ width: '100%', height: '250px' }"></div>
                    </div>
                    <div class="bottom">
                        <div class="actCodeList">
                            <div class="item" v-for="item in actCodeList" :key="item">
                                <div class="con">
                                    <div class="left">
                                        <img src="@/assets/agentImg.png" class="agentImg" alt="" />
                                    </div>
                                    <div class="right">
                                        <div class="name">{{ item.custName }}</div>
                                        <div class="phone">{{ item.phoneNo }}</div>
                                        <div class="phone">{{ item.incrDate }} {{ item.incrTime }}</div>
                                    </div>
                                </div>
                            </div>

                            <empty v-if="!actCodeList.length"></empty>
                        </div>
                    </div>
                </div>
            </van-tab>
        </van-tabs>

        <van-popup v-model:show="show" round position="bottom">
            <van-tabs v-model:active="active" color="#66d6ff">
                <van-tab title="按月">
                    <van-picker v-model="aa" title="标题" :columns="monList" :show-toolbar="false" />
                </van-tab>
                <van-tab title="按日">
                    <van-picker v-model="bb" :columns="dateList" @change="onChangeDate" :show-toolbar="false" />
                </van-tab>
            </van-tabs>
            <div class="btn" @click="endDate">确定</div>
        </van-popup>
    </div>
</template>

<script>
import * as echarts from 'echarts'
import {
    appRankStatisCustDayAndMonth,
    appRankStatisTerminalDayAndMonth,
    appStatisCustDayAndMonth,
    appStatisTerminalDayAndMonth,
    findByTargetAndClassesId,
    queryAllTimeRecently
} from '@/api/dataAnalysis'
export default {
    name: 'merchantsTj',
    data() {
        return {
            chartData: [],
            viewDate: '',
            show: false,
            isDirect: 0,
            active: 0,
            monList: [],
            monL: '',
            dateL: '',
            aa: [],
            bb: [],
            myChart: null,
            typeName: '',
            showSheet: false,
            actCode: '',
            typeList: [],
            actCodeList: [],
            dateList: [],
        }
    },
    mounted() {
        this.getQueryAllTimeRecently()
    },
    methods: {
        getQueryAllTimeRecently() {
            queryAllTimeRecently().then(res => {
                this.monList = res.object.map(item => {
                    for (const key in item) {
                        return { text: key,value:key }
                    }
                })
                res.object.forEach((element,index) => {
                    for(let key in element){
                        let data = []
                        for(let k in element[key]){
                            data.push({text: element[key][k],value: element[key][k]})
                        }
                        this.dateList.push({text: key,value: key, children:[...data]})
                    }
                });
                console.log('xxxxxxxxxxx', this.dateList);
                this.viewDate = this.monList[0].text
                this.getAppStatisTerminalDayAndMonth()
                this.getAppRankStatisCustDayAndMonth()
            })
        },
        onChangeMon(e) {
            console.log(e, 123)
        },
        aaa() {
            this.getAppStatisTerminalDayAndMonth()
            this.getAppRankStatisCustDayAndMonth()
        },
        endDate() {
            if (this.active === 0) {
                this.viewDate = this.aa[0]
            } else {
                this.viewDate = this.bb[0]+ '-'+ this.bb[1]
            }
            this.getAppStatisTerminalDayAndMonth()
            this.getAppRankStatisCustDayAndMonth()

            this.show = false
        },
        getAppStatisTerminalDayAndMonth() {
            appStatisCustDayAndMonth({
                searchDate: this.viewDate,
                isDirect: !this.isDirect ? 'Y' : 'N',
                currentPage: 1,
                dateType: !this.active ? 'MONTH' : 'DAY'
            }).then(res => {
                this.chartData = res.object.list
                const dateList = res.object.list.map(function (item) {
                    return item.incrDate
                })
                const valueList = res.object.list.map(function (item) {
                    return item.incrNum
                })
                this.showorders(dateList, valueList)
            })
        },
        getAppRankStatisCustDayAndMonth() {
            appRankStatisCustDayAndMonth({
                searchDate: this.viewDate,
                isDirect: !this.isDirect ? 'Y' : 'N',
                currentPage: 1,
                dateType: !this.active ? 'MONTH' : 'DAY'
            }).then(res => {
                this.actCodeList = res.object.list
            })
        },
        showorders(dateList, valueList) {
            // 基于准备好的dom，初始化echarts实例
            document.getElementById('myChart' + this.isDirect).setAttribute('_echarts_instance_', '')

            var chartDom = document.getElementById('myChart' + this.isDirect)
            if (this.myChart != null && this.myChart != '' && this.myChart != undefined) {
                this.myChart.dispose() //销毁
            }
            this.myChart = echarts.init(chartDom)
            // 指定图表的配置项和数据
            var option

            option = {
                xAxis: {
                    type: 'category',
                    data: dateList
                },

                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: valueList,
                        type: 'line',
                        smooth: true,
                        itemStyle: {
                            normal: {
                                color: '#eb3941', //改变折线点的颜色
                                lineStyle: {
                                    color: '#eb3941' //改变折线颜色
                                }
                            }
                        },
                    }
                ]
            }

            option && this.myChart.setOption(option)
        },
        onChangeDate(e) {
            this.dateL = e[0].text + '-' + e[1]
        },
        openPicker() {
            this.show = true
            this.monL = this.monList[0].text
            this.dateL = this.dateList[0].text + '-' + this.dateList[0].children[0].text
            console.log(this.dateL);
        }
    }
}
</script>

<style scoped lang="scss">
.wrapper {
    .myContent {
        padding: 10px;
    }

    .top,
    .bottom {
        background-color: #ffffff;
        padding: 10px;

        border-radius: 10px;

        .dateMon {
            font-size: 16px;
            font-weight: 400;
            color: #000000;
            opacity: 1;
            display: flex;
            align-items: center;

            img {
                width: 20px;
            }
        }

        .con {
            display: flex;
            align-items: center;
            padding: 10px;
            border-bottom: 1px solid var(--van-cell-border-color);

            .left {
                .agentImg {
                    width: 50px;
                }
            }

            .right {
                flex: 1;
                padding: 0 10px;
                line-height: 25px;

                .name {
                    font-size: 14px;
                }

                .phone {
                    font-size: 12px;
                    color: #9a9a9a;
                }

                .num {
                    font-size: 12px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                }
            }
        }

        .con:last-child {
            border: none;
        }
    }

    .bottom {
        margin-top: 10px;
    }
}

.btn {
    width: 159px;
    height: 40px;
    background: #66d6ff;
    opacity: 1;
    border-radius: 6px;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 400;
    color: #ffffff;
    margin: 20px auto;
}
</style>
